package com.eudon.ai.agent.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author eudon
 * @description list相关的工具类
 * @date 2025/8/15
 * @email syd19970616@gmail.com
 * @address <a href="https://gitee.com/eudon">Gitee</a>
 */
public class ListUtils {
    /**
     * 将一个list切分成多个指定大小的list
     *
     * @param list      原list
     * @param chunkSize 指定切分的大小
     * @param <T>       list 泛型
     * @return 切分后的list
     */
    public static <T> List<List<T>> partitionView(List<T> list, int chunkSize) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        if (chunkSize <= 0) {
            throw new IllegalArgumentException("chunkSize must be > 0");
        }
        int size = list.size();
        int chunks = (size + chunkSize - 1) / chunkSize;
        List<List<T>> result = new ArrayList<>(chunks);
        for (int i = 0; i < size; i += chunkSize) {
            int end = Math.min(i + chunkSize, size);
            result.add(list.subList(i, end)); // 视图
        }
        return result;
    }

    public static <T> List<List<T>> partitionCopy(List<T> list, int chunkSize) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        if (chunkSize <= 0) {
            throw new IllegalArgumentException("chunkSize must be > 0");
        }
        int size = list.size();
        int chunks = (size + chunkSize - 1) / chunkSize;
        List<List<T>> result = new ArrayList<>(chunks);
        for (int i = 0; i < size; i += chunkSize) {
            int end = Math.min(i + chunkSize, size);
            result.add(new ArrayList<>(list.subList(i, end))); // 拷贝
        }
        return result;
    }

}
